<?php

require_once( '../system/smarty.inc' );

require_once( '../system/mdao/Class_M_MATRIX_MST.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );

require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_MATRIX,$login['staff_auth']);

$dosearch = FALSE ;
$dao = new Class_M_MATRIX_MST;

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ||
	 isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) {
	$forms =  $_SESSION['MSEARCH']['MATRIX_MST'];
}else {
	$forms = $_GET;
	$_SESSION['MSEARCH']['MATRIX_MST'] = $forms;
}
//print_r( $forms );
$wherearr =  array();

/*
if ( isset( $forms['cd'] ) &&  ( !empty( $forms['cd'] ) ) ){
	$wherearr[] = sprintf(" cd =  '%s' ",  $forms['cd'] );
}
*/
if ( isset( $forms['sort'] ) &&  ( !empty( $forms['sort'] ) ) ){
	$wherearr[] = sprintf(" sort =  '%s' ",  $forms['sort'] );
}
/*
if ( isset( $forms['name'] ) &&  ( !empty( $forms['name'] ) ) ){
	$wherearr[] = sprintf(" name like  '%s' ",  "%".$forms['name']."%" );
}
*/

//---CSVダウンロード　処理開始
if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
	$_GET["csv"] = "";
	$tmpfilename = "ins_mservice".date("Ymd_His");
	header("Content-Type: application/octet-stream"); 
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	
	$colums = array();
	//列名
	
	$col_heads = array(
		'cd'            => array( 'コード' ,     1),
		'name'          => array( '名称' ,       1),
		'u_time'        => array( '更新日時' ,   3),
		'm_user_name'   => array( '更新者' ,     1)
	);
	
	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}
	$data = $dao->search( $wherearr );
	
	foreach ($data as $k => $v) {
		$data[$k]["u_time"]      =  Class_PWD::dateTime_to_YmdHi($v["u_time"]);
	}
	$dao->output_csv($data, $colums);
	exit;
}
//---CSVダウンロード　処理完了

if ( isset( $forms['search'] ) && (  !empty( $forms['search'] ) ) ){
	$dosearch = TRUE;
}

if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){
	$dosearch = TRUE;
	$page['current'] = $forms['page'];	
}else{
	$page['current'] = 1;
}
$page['item'] = PAGE_MAX;
if ($dosearch) {
	//$page内容は関数内部更新されます。
	$data = $dao->search( $wherearr, $page );
}
if (empty($forms["sort"])){
	$forms["sort"] = '1';
}

if( isset($forms) ) $smarty->assign('forms', $forms);
if( isset($data) )  $smarty->assign('data', $data);
if( isset($page) )  $smarty->assign('page', $page);

$smarty->assign('sorts', $aoki_mst_sort);

$smarty->assign('menu',  $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('mmatrix_master_index.html');

?>